import { darkCssIsReady, loadDarkThemeCss } from '@rys-fe/vite-plugin-theme/es/client';
import { addClass, hasClass, removeClass } from '/@/utils/domUtils';
import { useAppStore } from '/@/store/modules/app'
export async function updateDarkTheme(mode: string | null = 'light') {
  const app = useAppStore()
  const htmlRoot = document.getElementById('htmlRoot');

  // app.setDarkMode(mode=== 'light' ? 'dark' : 'light')

  // if (!htmlRoot) {
  //   return;
  // }
  // const hasDarkClass = hasClass(htmlRoot, 'dark');
  if (mode === 'dark') {
    if (import.meta.env.PROD && !darkCssIsReady) {
      await loadDarkThemeCss();
    }
    // htmlRoot.setAttribute('data-dark', 'dark');
    
    // if (!hasDarkClass) {
    //   addClass(htmlRoot, 'dark');
    // }
    app.setDarkMode('dark')
    document.documentElement.setAttribute('data-dark', 'dark')
  } else {
    app.setDarkMode('light')
    document.documentElement.setAttribute('data-dark', 'light')
    // htmlRoot.setAttribute('data-dark', 'light');
    // if (hasDarkClass) {
    //   removeClass(htmlRoot, 'dark');
    // }
  }
}

